package com.ssyc.gsk_teacher.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.makeramen.roundedimageview.RoundedImageView;
import com.ssyc.common.glide.ImageUtil;
import com.ssyc.common.manager.PickChooseManager;
import com.ssyc.common.utils.ScreenUtils;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.gsk_teacher.R;
import com.ssyc.gsk_teacher.activity.TeacherNoticeDetailsActivity;
import com.ssyc.gsk_teacher.bean.NoticeInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 公告列表列表适配器
 * Created by Administrator on 2018/6/20 0020.
 */

public class NoticeRvAdapter extends BaseQuickAdapter<NoticeInfo.ListBean, BaseViewHolder> {

    private Context context;
    private LinearLayoutManager linearLayoutManager;
    private LinearLayout.LayoutParams lp;
    private RvAdapter rvAdapter;

    public NoticeRvAdapter(Context context, int layoutResId, @Nullable List<NoticeInfo.ListBean> data) {
        super(layoutResId, data);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, NoticeInfo.ListBean item) {
        //时间
        TextView tvTime = helper.getView(R.id.tv_time);
        tvTime.setText(item.getDate());
        //名称
        TextView tvName = helper.getView(R.id.tv_name);
        tvName.setText(item.getTag());
        //详细时间
        TextView tvDetailTime = helper.getView(R.id.tv_detail_time);
        tvDetailTime.setText(item.getTime());
        //内容
        TextView tvContent = helper.getView(R.id.tv_content);
        tvContent.setText(item.getContent());

        //展示图片的RecyclerView
        RecyclerView rvShowPic = helper.getView(R.id.rv_show_pic);

        //获取需要展示的图片数据
        List picData = new ArrayList<>();
        if (!TextUtils.isEmpty(item.getPicture1())) {
            picData.add(item.getPicture1());
        }
        if (!TextUtils.isEmpty(item.getPicture2())) {
            picData.add(item.getPicture2());
        }
        if (!TextUtils.isEmpty(item.getPicture3())) {
            picData.add(item.getPicture3());
        }
        if (!TextUtils.isEmpty(item.getPicture4())) {
            picData.add(item.getPicture4());
        }
        if (!TextUtils.isEmpty(item.getPicture5())) {
            picData.add(item.getPicture5());
        }
        if (!TextUtils.isEmpty(item.getPicture6())) {
            picData.add(item.getPicture6());
        }
        if (!TextUtils.isEmpty(item.getPicture7())) {
            picData.add(item.getPicture7());
        }
        if (!TextUtils.isEmpty(item.getPicture8())) {
            picData.add(item.getPicture8());
        }
        if (!TextUtils.isEmpty(item.getPicture9())) {
            picData.add(item.getPicture9());
        }

        if (picData.size() == 0) {
            rvShowPic.setVisibility(View.GONE);
        } else {
            rvShowPic.setVisibility(View.VISIBLE);
            //设置数据
            LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
            rvShowPic.setLayoutManager(manager);
            rvShowPic.setHasFixedSize(true);
            rvAdapter = new RvAdapter(R.layout.base_common_item_rv_notice_show_pic, picData);
            rvShowPic.setAdapter(rvAdapter);

            //点击查看大图
            rvAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    //点击查看大图
                    if (null == picData || picData.size() == 0) {
                        return;
                    }
                    PickChooseManager.startShowBigActivity(context, picData, position);
                }
            });
        }

        //查看详情的布局
        RelativeLayout rl = helper.getView(R.id.rl_details);
        rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到查看详情的界面
                Intent intent = new Intent(context, TeacherNoticeDetailsActivity.class);
                intent.putExtra("data", item);
                context.startActivity(intent);
            }
        });
    }

    /**
     * 展示列表的
     */
    class RvAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

        private LinearLayout.LayoutParams lp;

        private RelativeLayout.LayoutParams lpLv;

        public RvAdapter(int layoutResId, @Nullable List<String> data) {
            super(layoutResId, data);
            initParams(context);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
            RelativeLayout rlOut = helper.getView(R.id.rl_out);
            rlOut.setLayoutParams(lp);
            RoundedImageView lv = helper.getView(R.id.iv);
            lv.setLayoutParams(lpLv);
            ImageUtil.displayImage(context, lv, item);
        }

        /**
         * 配置图片的布局参数
         *
         * @param context
         */
        private void initParams(Context context) {
            //计算最外层的大小
            int screenWidth = ScreenUtils.getScreenWidth(context);
            int outWidth = (screenWidth - UiUtils.dp2px(8 * 3)) / 3;
            int outHeight = outWidth;
            lp = new LinearLayout.LayoutParams(outWidth, outHeight);
            //计算图片大小
            int ivWidth = (screenWidth - UiUtils.dp2px(16 * 2 + 8 * 2)) / 3;
            int ivHeight = ivWidth;
            lpLv = new RelativeLayout.LayoutParams(ivWidth, ivHeight);
            lpLv.addRule(RelativeLayout.CENTER_IN_PARENT);
        }
    }

}
